package com.example.as_sum.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.as_sum.activity.BaseActivity;
import com.example.as_sum.util.Log;
import com.example.as_sum.view.CustomProgressDialog;

/**
 * Created by ran on 2016/7/20.
 */
public abstract class RootFragment extends Fragment {
    public BaseActivity bContext;
    final String tga=this.getClass().getSimpleName();
    private CustomProgressDialog dialog;

    /**
     * 界面开始初始化
     */
    protected boolean isViewInitiated;
    /**
     * 界面是否可见
     */
    protected boolean isVisibleToUser;
    /**
     * 数据是否填充完毕
     */
    protected boolean isDataInitiated;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        bContext=(BaseActivity)context;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e(tga,"onCreate");
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isViewInitiated = true;
        prepareFetchData();
        Log.e(tga,"onActivityCreated");
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        prepareFetchData();
        if(isVisibleToUser)
            Log.e(tga,"setUserVisibleHint:true");
        else
            Log.e(tga,"setUserVisibleHint:false");
    }

    public abstract void fetchData();

    public boolean prepareFetchData() {
        return prepareFetchData(true);
    }

    /**
     * 是否强制更新
     * @param forceUpdate  true是只要可见就刷新
     * @return
     */
    public boolean prepareFetchData(boolean forceUpdate) {
        if (isVisibleToUser && isViewInitiated && (!isDataInitiated || forceUpdate)) {
            fetchData();
            isDataInitiated = true;
            return true;
        }
        return false;
    }
    public void showDialog(String text, boolean b) {

        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
        dialog = new CustomProgressDialog(bContext);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(b);

        if (!dialog.isShowing()) {
            dialog.show();
        }

        dialog.setText(text);

    }
    public void closeDialog() {
        if (dialog != null) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.e(tga,"onCreateView");
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.e(tga,"onStart");
    }
}
